﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace AssemblyLoad
{
    class AssemblyLoad
    {
        public AssemblyLoad() { }

        /// <summary>
        /// 反射加载类
        /// </summary>
        /// <param name="classname"></param>
        /// <param name="par"></param>
        /// <returns></returns>
        public string plugin(string dllname ,string classname,object[] par) {
            string path = Path.GetDirectoryName(Application.ExecutablePath); //目录指向exe的目录
            byte[] filesByte = File.ReadAllBytes(Path.GetDirectoryName(Application.ExecutablePath) + "//Plugin//" + dllname + ".dll"); //指定exe目录中的ddl的位置
            Assembly assembly = Assembly.Load(filesByte);//Assembly.Load 会加载所有的相关的dll
            Type type = assembly.GetType("dynamic.classname");//加载命名空间与类名称
            MethodInfo timerStart = type.GetMethod(classname);
            string returnobj = "";
            if (timerStart != null)
            {
                object obj = System.Activator.CreateInstance(type, true);//加载类
                returnobj = (string)timerStart.Invoke(obj, par);
            }
            return returnobj;
        }

        public string tostring() {
            return "OK";
        }
    }
}
